﻿using System;

namespace Triples {


    public struct Card : ICard {
        public readonly byte InnerValue;

        public Shades Shade {
            get { return (Shades) GetTrit(InnerValue, 0); }
        }

        public Shapes Shape {
            get { return (Shapes)GetTrit(InnerValue, 1); }
        }

        public Colors Color {
            get { return (Colors)GetTrit(InnerValue, 2); }
        }

        public Numbers Number {
            get { return (Numbers)GetTrit(InnerValue, 3); }
        }


        public Card (byte cardValue) {
            InnerValue = cardValue;
        }


        private int GetTrit (byte value, int tritIdx) {
            value %= Convert.ToByte (Math.Pow (3, tritIdx + 1));
            value /= Convert.ToByte (Math.Pow (3, tritIdx));
            return value;
        }


        public override bool Equals(object obj) {
            return (obj is Card) ? ((Card) obj).InnerValue == this.InnerValue : false;
        }


        public override int GetHashCode() {
            return InnerValue;
        }


        public override string ToString() {
            return String.Format ("{0} {1} {2} {3}\r\n", Number, Shape, Color, Shade);
        }
    }
}